<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-08-04 15:25:46
 * @version $Id$
 */
namespace Wx\Wxpay;

require_once "lib/WxPay.Data.php";
require_once 'lib/WxPay.Api.php' ;
require_once 'WxPay.tools.php' ;

class WxpaySdk {
    

    function __construct($opt){
    	$this->config=$opt;
    	$this->WxPayApi = new \WxPayApi($this->config);
    	$this->WxPayNotify = new \WxPayNotifyReply();
    }

    public function xcxPay($param){
		$body         = isset($param['body'])?$param['body']:'';
		$attach       = isset($param['attach'])?$param['attach']:'';
		$out_trade_no = $param['out_trade_no'];
		$time_start   = isset($param['time_start'])?$param['time_start']:'';
		$time_expire  = isset($param['time_expire'])?$param['time_expire']:'';
		$goods_tag    = isset($param['goods_tag'])?$param['goods_tag']:'';
		$notify_url   = isset($param['notify_url'])?$param['notify_url']:$this->config['notify_url'];
		$openid       = $param['openid'];
		$total_fee    = isset($param['total_fee'])?$param['total_fee']:0;
		$trade_type   = isset($param['trade_type'])?$param['trade_type']:'JSAPI';

		//②、统一下单
    	$input = new \WxPayUnifiedOrder($this->config);
		$input->SetBody($body);
		$attach && $input->SetAttach($attach);
		$input->SetOut_trade_no($out_trade_no);
		$input->SetTotal_fee($total_fee);
		$time_start && $input->SetTime_start(date("YmdHis",strtotime($time_start)));
		$time_expire && $input->SetTime_expire(date("YmdHis",strtotime($time_expire)));
		$goods_tag && $input->SetGoods_tag($goods_tag);
		$input->SetNotify_url($notify_url);
		$input->SetTrade_type($trade_type);
		$input->SetOpenid($openid);
		
		// $WxPayApi = new \WxPayApi($this->config);
		$order = $this->WxPayApi->unifiedOrder($input);
		return $order;
    }

    public function GetJsApiParameters($order){
    	$tools = new \WxpayTools($this->config);
		$jsApiParameters = $tools->GetJsApiParameters($order);
		return $jsApiParameters;
    }

    /**
 	 * 
 	 * 支付结果通用通知
 	 * @param function $callback
 	 * 直接回调函数使用方法: notify(you_function);
 	 * 回调类成员函数方法:notify(array($this, you_function));
 	 * $callback  原型为：function function_name($data){}
 	 */
	public function notify($callback,$needSign=false)
	{
		
		$msg = "OK";
		//当返回false的时候，表示notify中调用NotifyCallBack回调失败获取签名校验失败，此时直接回复失败
		$result = $this->WxPayApi->notify($callback, $msg);
		if($result == false){
			$this->WxPayNotify->SetReturn_code("FAIL");
			$this->WxPayNotify->SetReturn_msg($msg);
			$this->ReplyNotify(false);
			return;
		} else {
			//该分支在成功回调到NotifyCallBack方法，处理完成之后流程
			$this->WxPayNotify->SetReturn_code("SUCCESS");
			$this->WxPayNotify->SetReturn_msg("OK");
		}
		$this->ReplyNotify($needSign);
	}

	/**
	 * 
	 * 回复通知
	 * @param bool $needSign 是否需要签名输出
	 */
	final private function ReplyNotify($needSign = true)
	{
		//如果需要签名
		if($needSign == true && 
			$this->WxPayNotify->GetReturn_code('return_code') == "SUCCESS")
		{
			$this->WxPayNotify->SetSign();
		}
		$this->WxPayApi->replyNotify($this->WxPayNotify->ToXml());
	}

	/**
	 * 订单查询接口
	 * @return [type] [description]
	 */
	public function orderQuery($param){
    	$input = new \WxPayOrderQuery($this->config);
		$input->SetOut_trade_no($param['order_no']);
		$input->SetTransaction_id($param['pay_no']);
		$resoult = $this->WxPayApi->orderQuery($input);
		return $resoult;
	}

	/**
	 * 关闭订单
	 * @param  [type] $param [description]
	 * @return [type]        [description]
	 */
	public function closeOrder($param)
	{
		$input = new \WxPayCloseOrder($this->config);
		$input->SetOut_trade_no($param['order_no']);
		$resoult = $this->WxPayApi->closeOrder($input);
		return $resoult;
	}

	/**
	 * 退款
	 * out_refund_no、total_fee、refund_fee、op_user_id为必填参数
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	public function refund($param)
	{
		$input = new \WxPayRefund($this->config);
		$input->SetTransaction_id($param['transaction_id']);
		$input->SetOut_refund_no($param['out_refund_no']);
		$input->SetTotal_fee($param['total_fee']);
		$input->SetRefund_fee($param['refund_fee']);
		$input->SetOp_user_id($param['op_user_id']);
		$resoult = $this->WxPayApi->refund($input);
		return $resoult;
	}

}